<template>
    <el-container>
        <el-header height="80px" style="padding: 10px;">
            <!-- 工具/搜索栏 -->
            <div style="padding: 5px; background: #F2F6FC; border-radius: 5px;">
                <div class="topbar">
                    <div class="left-panel">
                        <el-button type="success" icon="el-icon-plus" @click="save()">添加注册中心</el-button>
                    </div>
                    <div class="right-panel">
                        <div class="right-panel-search">
                            <el-input v-model="searchForm.searchText" placeholder="搜索关键词..." style="width: 200px;"></el-input>
                            <el-button type="primary" icon="el-icon-search" @click="getList()"></el-button>
                            <el-button type="warning" icon="el-icon-refresh" @click="refresh()"></el-button>
                        </div>
                    </div>
                </div>
            </div>       
        </el-header>

        <el-main style="padding-left: 10px; padding-right: 10px;">
            <el-card shadow="always" body-style="padding: 0px; min-height: 500px;">
                <el-table
                ref="Table"
                :data="pageResult.list"
                element-loading-text="加载数据中"
                v-loading="loading"
                :header-row-style="{height:45+'px'}"
                :row-style="{height:60+'px'}"
                style="width: 100%; min-height: 450px;"
                :header-cell-style="{background:'#F2F6FC'}">
                    <el-table-column prop="regCenterName"  label="注册中心名称" ></el-table-column>
                    <el-table-column prop="regCenterType"  label="注册中心类型" ></el-table-column>
                    <el-table-column prop="regCenterAddr"  label="注册中心地址" ></el-table-column>
                    <el-table-column prop="status"     label="状态" >
                        <template slot-scope="scope">
                            <el-tag type="success" v-if="scope.row.status">默认</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column prop="createTime"  label="创建时间" ></el-table-column>
                    <el-table-column prop="createUser"  label="创建人" ></el-table-column>
                    <el-table-column label="操作" width="200">
                        <template slot-scope="scope">
                            <el-button
                                type="text"
                                style="color: #409EFF"
                                icon="el-icon-edit"
                                @click="update(scope.$index, scope.row)">编辑</el-button> 
                            <el-button
                                type="text"
                                style="color: #67C23A"
                                icon="el-icon-open"
                                v-if="!scope.row.status"
                                @click="modifyStatus(scope.$index, scope.row)">设为默认</el-button>  
                        </template>
                    </el-table-column>
                    <template #empty>
                        <el-empty :description="'查无数据'" :image-size="100"></el-empty>
                    </template>
                </el-table>
                <pagination :total="pageResult.total" @page-change="pageChange"></pagination>
            </el-card>
            
        </el-main>

        <save-update-config ref="SaveUpdateConfig" @success="success"></save-update-config>
    </el-container>
</template>

<script>
import SaveUpdateConfig from './modules/add_update_config.vue'

export default {
    components: {
        SaveUpdateConfig
    },
    data() {
        return {
            searchForm: {
                searchText: ''
            },
            pageResult: { 
                total: 0,
                pageNum: 1,
                pageSize: 10,
                list: []
            },
            loading: false,
            visibleViewsDialog: false,
            currentRow: {},
        }
    },
    mounted() {       
        // 初始化列表
        this.getList()
    },
    methods: {
        /**
         * @title 获取列表
         */
        async getList() {
            this.loading = true
            let response = await this.$API.regcenterConfig.getList(this.searchForm)   
            this.pageResult = response.data
            this.loading = false         
        },

        refresh() {
            this.searchForm.searchText = ''
            this.getList()
        },

        /**
         * @title 更改分页
         */
        pageChange(pageNum, pageSize) {
            this.searchForm.pageNum = pageNum
            this.searchForm.pageSize = pageSize
            this.getList()
        },
       
        save() {
            this.$nextTick(() => {
                this.$refs.SaveUpdateConfig.open({'regCenterType': 'EUREKA'}) 
            })
        },

        update(index, row) {
            this.$nextTick(() => {
                this.$refs.SaveUpdateConfig.open(row) 
            })
        },

        modifyStatus(index, row) {
            let opMsg = "确定将该注册中心 [" + row.regCenterName + '] ' +'设为默认吗?'
            this.$confirm(opMsg, '操作提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                center: true
            }).then(() => {
                
                let data = {
                    'id': row.id
                }

                this.$API.regcenterConfig.modifyStatus(data).then(response => {
                    this.$message.success(response.message)
                    this.getList()
                })

            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消操作'
                })
            })
        },

        success(id) {
            this.getList()
        }
    }
}
</script>

<style lang="scss" scoped>
.el-main {
    width: 100%;
    height: 100%;
    padding: 0px;
}
.topbar {
    height: 50px; 
    display: flex;
    justify-content:space-between;
    margin-bottom: 0px;

    .left-panel {
        display: flex;
        align-items: center;
    }
    .right-panel {
        display: flex;
        align-items: center;
    }

    .right-panel-search {display: flex;align-items: center;}

    .right-panel-search > * + * {margin-left:10px;}
}


</style>